package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivitySelVideoBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type;
    private SelVideoAdapter videoAdapter;
    private List<String> listSel = new ArrayList<>();
    private String selPath = "";
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSelVideoComplete.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 4));
        videoAdapter = new SelVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSelVideoComplete:
                if (listSel == null && selPath == null) {
                    ToastUtils.showShort(getString(R.string.please_sel_pic));
                    return;
                }
                if (type == 0) {
                    saveFileToAlbum();
                } else if (type == 1) {
                    GifMakeActivity.sVideoPath = selPath;
                    startActivity(GifMakeActivity.class);
                } else if (type == 2) {
                    VideoSpeedActivity.videoPath = selPath;
                    startActivity(VideoSpeedActivity.class);
                } else if (type == 3) {
                    VideoUpendActivity.videoPath = selPath;
                    startActivity(VideoUpendActivity.class);
                } else if (type == 4) {
                    VideoCutActivity.videoPath = selPath;
                    startActivity(VideoCutActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (position == 0) {
            SelCameraActivity.type = 1;
            startActivity(SelCameraActivity.class);
            return;
        }
        if (type == 0) {
            if (videoAdapter.getItem(position).isChecked()) {
                videoAdapter.getItem(position).setChecked(false);
                listSel.remove(videoAdapter.getItem(position).getPath());
            } else {
                videoAdapter.getItem(position).setChecked(true);
                listSel.add(videoAdapter.getItem(position).getPath());
            }
            videoAdapter.notifyItemChanged(position);
        } else {
            videoAdapter.getItem(oldPosition).setChecked(false);
            videoAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            videoAdapter.getItem(position).setChecked(true);
            videoAdapter.notifyItemChanged(position);
            selPath = videoAdapter.getItem(position).getPath();
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_video_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selectMediaEntities.add(0, new SelectMediaEntity());
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //保存选择文件到相册
    private void saveFileToAlbum() {
        showDialog(getString(R.string.add_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                List<MyAlbumBean> listSp = AppSpUtils.getVideoAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    for (MyAlbumBean bean : listSp) {
                        if (bean.getId().equals(VideoAlbumActivity.albumId)) {
                            if (bean.getListData() != null && bean.getListData().size() > 0) {
                                bean.getListData().addAll(listSel);
                            } else {
                                bean.setListData(listSel);
                            }
                            break;
                        }
                    }
                    AppSpUtils.saveVideoAlbumList(listSp);
                    emitter.onNext("");
                }
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                finish();
            }
        });
    }

}